package cn.itcast.mq.listen;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @作者 itcast
 * @创建日期 2021/11/1 11:05
 **/
@Component
@Slf4j
public class SpringRabbitListener {
    //    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleMsg(String msg){
//        log.info("消费者接收到消息 ==> 【{}】",msg);
//    }
    private static long work1Num = 0 ;
    private static long work2Num = 0 ;
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue01(String msg) throws InterruptedException {
        work1Num++;
        log.warn("消费者1 接收到消息 ==> 【{}】  处理消息数量:{}", msg,work1Num);
        TimeUnit.MILLISECONDS.sleep(20);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue02(String msg) throws InterruptedException {
        work2Num++;
        log.warn("消费者2 接收到消息 ==> 【{}】  处理消息数量:{}", msg,work2Num);
        TimeUnit.MILLISECONDS.sleep(200);
    }


    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue01(String msg) throws InterruptedException {
        log.warn("消费者1 接收到消息fanout消息 ==> 【{}】", msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue02(String msg) throws InterruptedException {
        log.warn("消费者2 接收到消息fanout消息 ==> 【{}】", msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),   // 队列
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), // 交换机
            key = {"blue","red"}  // 路由key
    ))
    public void listenDirectQueue01(String msg) throws InterruptedException {
        log.warn("消费者1 接收到消息direct消息 ==> 【{}】", msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),   // 队列
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT), // 交换机
            key = {"yellow","red"}  // 路由key
    ))
    public void listenDirectQueue02(String msg) throws InterruptedException {
        log.warn("消费者2 接收到消息direct消息 ==> 【{}】", msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),   // 队列
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), // 交换机
            key = {"china.#"}  // 路由key
    ))
    public void listenTopicQueue01(String msg) throws InterruptedException {
        log.warn("消费者1 接收到消息topic消息 ==> 【{}】", msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),   // 队列
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC), // 交换机
            key = {"#.news"}  // 路由key
    ))
    public void listenTopicQueue02(String msg) throws InterruptedException {
        log.warn("消费者2 接收到消息topic消息 ==> 【{}】", msg);
    }
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue01(Map map) throws InterruptedException {
        log.warn("消费者1 接收到消息 ==> 【{}】  处理消息数量:{}", map);
    }
}
